﻿using SZJT.Framework.Services.Core;
using SZJT.Framework.Services;
using SZJT.Framework.Core;
using SZJT.Framework;
using System.Text;
using System.Web;
using System.Net;
using System;

namespace SZJT.Framework.Services
{
    public static class ServerInfoService
    {
        public static string GetData(string taskGuid, string dataGuid, string dataType)
        {
            if (taskGuid != "e32cc6ac-199c-4a6f-93f8-937d3e482d13")
            {
                throw new Exception("获取系统信息时TaskGuid不一致：e32cc6ac-199c-4a6f-93f8-937d3e482d13<>" + taskGuid);
            }
            if (dataGuid != "dffb35bb-a00a-4438-934d-90f2d79e0440")
            {
                throw new Exception("获取系统信息时DataGuid不一致：dffb35bb-a00a-4438-934d-90f2d79e0440<>" + dataGuid);
            }
            if (dataType != "ServerInfo")
            {
                throw new Exception("获取系统信息时DataType不一致：ServerInfo<>" + dataType);
            }
            var localCert = CertService.GetLocalCert();
            var sb = new StringBuilder();
            sb.Append(new XmlHelper.XmlElement(Constants.SingleMessageRootName)
            .AddAttribute(Constants.TaskGuidName, taskGuid)
            .AddAttribute(Constants.DataGuidName, dataGuid)
            .AddAttribute(Constants.DataTypeName, dataType)
            .ToString());

            sb.Append(new XmlDataElement()
            {
                Name = "ServerName",
                ServiceDataType = Framework.Services.ServiceDataType.TEXT,
                Value = GetHostName()
            });
            sb.Append(new XmlDataElement { Name = "AccountName", ServiceDataType = ServiceDataType.TEXT, Value = "" });
            sb.Append(new XmlDataElement
            {
                Name = "ServiceGuid",
                ServiceDataType = ServiceDataType.TEXT,
                Value = ""
            });
            sb.Append(new XmlDataElement
            {
                Name = "PublicKey",
                ServiceDataType = ServiceDataType.TEXT,
                Value = CertService.GetPublicKey(localCert)
            });
            sb.Append(new XmlDataElement
            {
                Name = "TaskList",
                ServiceDataType = ServiceDataType.TEXT,
                Value = SZJT.Framework.Adapter.AdapterFactory.TaskListStr
            });
            sb.Append(XmlHelper.GetEndElementString(Constants.SingleMessageRootName));
            return sb.ToString();
        }


        private static string GetHostName()
        {
            return HttpContext.Current == null ? Dns.GetHostName() : HttpContext.Current.Request.ServerVariables["Server_Name"];
        }
    }
}